'''
Created on Feb 25, 2011

@author: Chris
'''
import pickle
import string
import Feature
import Suggestion
import FeatureSuggestions
import DocumentTokenizer
import en
import nltk_contrib
import WordCount
import AvgSyllablesPerWord
import SentenceLength
import AvgSentencesPerParagraph
import AvgWordLength

class FeatureHandler(object):
    '''
    provides an easy way to manage manage features. Features used will be gotten through here and features will be analyzed and suggestions will be created and assigned accordingly 
    '''

    def __init__(self):
   
        '''
        Constructor
        '''
        self.features = []
        self.currentMatch = 0 # This is the % identification match for an author to his document based on the corpus
        self.lastMatch = 0 # This is the rough, immediate % identification match feedback for an author to his document based on the corpus
        self.document = ''
	
    def setDocument(self, doc):
        self.document = doc
        documentTokenized = DocumentTokenizer.DocumentTokenizer().tokenizeDocument(str(doc))
        self.documentSections = documentTokenized.getSections()
    
    def buildMatrix(self):
        '''Build the decision tree from the data table matrix'''
        
    def buildTree(self):
        '''Compose a matrix from the features'''
           
    '''def createSuggestion(self):'''
    def createSuggestion(self, features, revision):
        """
        This method takes a list of features and iterates through them, creating a list of suggestions and returning this list of features with suggestions. 
        This method should call the StyloHandler. 
        """
        
        #problem here - revision is a string, so what is this line supposed to do?
        self.document = revision.getText()
        self.documentAsString = self.document.get('1.0', 'end')
        
        self.features = features
        self.featureSuggestionsList = []
        
        '''Tokenize the document using DocumentTokenizer'''
        documentTokenized = DocumentTokenizer.DocumentTokenizer().tokenizeDocument(str(self.documentAsString))
        '''For each feature in self.features'''
        '''Get the name of the feature'''
        
        '''Get lists of sections, sentences, and words in the current Document'''
        '''Document contains a list of sections
        Sections contains a list of sentences
        Sentences contain a list of words'''
        
        self.documentSections = documentTokenized.getSections()
        
        for feature in range(len(self.features)):
            thisFeature = self.features[feature].feature
            thisValue = self.features[feature].value
            thisWeight = self.features[feature].description
            
            # We should do something like this in order to dynamically create features later
            #featureClass = self.features[feature].feature()
			
            if thisFeature == 'Author':
                self.currentMatch = round(float(thisWeight) * 100.00)
                self.currentAuthor = thisValue
			
            elif thisFeature == 'UniqueWordCount':
                uniqueWordValue = thisValue
                #coprusUniqueValue = 25 #This should be gotten from the new features from stylo
			
            elif thisFeature == 'WordCount':
                coprusWordValue = 50
                #tempFeature = WordCount.WordCount(self, feature, uniqueWordValue, coprusUniqueValue, coprusWordValue)
                #featureSuggestions = tempFeature.getFeatureSuggestions()
                #self.featureSuggestionsList = self.featureSuggestionsList + featureSuggestions
                
            elif thisFeature == 'AvgSyllables':
                tempFeature = AvgSyllablesPerWord.AvgSyllablesPerWord(self, feature, thisValue)
                featureSuggestions = tempFeature.getFeatureSuggestions()
                self.featureSuggestionsList = self.featureSuggestionsList + featureSuggestions
                
            elif thisFeature == 'AvgSentenceLength':
                tempFeature = SentenceLength.SentenceLength(self, feature)
                tempFeature.suggest(thisValue, 3)
                featureSuggestions = tempFeature.getFeatureSuggestions()
                self.featureSuggestionsList = self.featureSuggestionsList + featureSuggestions
                
            elif thisFeature == 'AvgSentencesPerParagraph':
                tempFeature = AvgSentencesPerParagraph.AvgSentencesPerParagraph(self, feature, thisValue)
                featureSuggestions = tempFeature.getFeatureSuggestions()
                self.featureSuggestionsList = self.featureSuggestionsList + featureSuggestions
				
            elif thisFeature == 'AvgWordLength':
                coprusWordLength = 5
                #tempFeature = AvgWordLength.AvgWordLength(self, self.features[feature], coprusWordLength)
                #featureSuggestions = tempFeature.getFeatureSuggestions()
                #self.featureSuggestionsList = self.featureSuggestionsList + featureSuggestions
				
                
        return self.featureSuggestionsList
        
    def calculateMatch(self):
        self.stylo.submitDocument(self.document)
        #This should return stuff that we don't have yet
        
if __name__ == '__main__': 
    driver = FeatureHandler()
    driver.createSuggestion()
